<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<c:set var="webroot" value="${pageContext.request.contextPath}" />
<html>
<head>
	<meta charset="utf-8">
	<title>新增借款</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="${webroot}/static/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="${webroot}/static/css/public.css" media="all" />
	<style type="text/css">
	  .layui-input, .layui-textarea {
	    display: inline;
        width: 70%;
        padding-left: 10px;
       }
       .layui-form-select .layui-edge {
    position: absolute;
    right: 107px;
    top: 50%;
    margin-top: -3px;
    cursor: pointer;
    border-width: 6px;
    border-top-color: #c2c2c2;
    border-top-style: solid;
    transition: all .3s;
    -webkit-transition: all .3s;
}
.layui-form-select dl {
    min-width: 70%;
}
.layui-col-md4 {
    width: 45.333333% !important;
}
.layui-form-select .layui-edge{
  right: 158px;
}
</style>
</head>
<body class="childrenBody">
<form class="layui-form" style="width:80%;">
	<input type="hidden" id="id" name="id">
	<div class="layui-row">
	    <div class="magb15 layui-col-md4 layui-col-xs12">
			<label class="layui-form-label"><span style="color:red;">*</span>借款方:</label>
			<div class="layui-input-block">
				 <input type="text" class="layui-input userId" disabled="disabled" lay-verify="required" placeholder="请选择借款方">
			<a class="layui-btn layui-btn layui-btn-normal" data-type="reload">选择</a>
			</div>
		</div>
	    <div class="magb15 layui-col-md4 layui-col-xs12">
            <label class="layui-form-label"><span style="color:red;">*</span>借款总金额:</label>
		    <div class="layui-input-block">
			    <input type="text" class="layui-input totalMoney" lay-verify="required|totalMoney" placeholder="请输入500-5000000的整数">
		    </div>
		</div>
	</div>
	<div class="layui-row">
	    <div class="magb15 layui-col-md4 layui-col-xs12">
		    <label class="layui-form-label"><span style="color:red;">*</span>总利息:</label>
		    <div class="layui-input-block">
			    <input type="text" class="layui-input totalAccrual" lay-verify="required|totalAccrual" placeholder="请输入总利息">
		    </div>
	    </div>
	    <div class="magb15 layui-col-md4 layui-col-xs12">
		    <label class="layui-form-label"><span style="color:red;">*</span>借款日期:</label>
		    <div class="layui-input-block">
		        <input type="text" class="layui-input" id="startTime" lay-verify="date" placeholder="请选择借款日期" lay-key="1">
		    </div>
	    </div>
	</div>
	<div class="layui-row">
	    <div class="magb15 layui-col-md4 layui-col-xs12">
		    <label class="layui-form-label"><span style="color:red;">*</span>还款日期:</label>
		    <div class="layui-input-block">
			    <input type="text" class="layui-input" id="endTime" lay-verify="date" placeholder="请选择还款日期" lay-key="2">
		    </div>
	    </div>
	    <div class="magb15 layui-col-md4 layui-col-xs12">
		    <label class="layui-form-label"><span style="color:red;">*</span>渠道来源:</label>
		<div class="layui-input-block newsStatus">
			<select name="channelId" lay-verify="required">
			<option value="">请选择渠道来源</option>
			<c:forEach items="${channelList}" var="l">
			    <option value="${l.id}">${l.channelName}</option>
			</c:forEach>
			</select>
		</div>
	    </div>
	</div>
	<div class="layui-row">
	    <div class="magb15 layui-col-md4 layui-col-xs12">
		    <label class="layui-form-label"><span style="color:red;">*</span>放贷平台:</label>
		   <div class="layui-input-block newsStatus">
		    <select name="lendingId" lay-verify="required">
			<option value="">请选择放贷平台</option>
			<c:forEach items="${lendingList}" var="s">
			    <option value="${s.id}">${s.lendingName}</option>
			</c:forEach>
			</select>
	      </div>
	    </div>
	    <div class="magb15 layui-col-md4 layui-col-xs12">
		    <label class="layui-form-label"><span style="color:red;">*</span>业务员:</label>
		    <div class="layui-input-block">
			    <input type="text" class="layui-input businessMan" lay-verify="required" placeholder="请输入业务员">
		    </div>
	    </div>
	</div>
	<div class="layui-form-item layui-row layui-col-xs12">
		<div class="layui-input-block">
			<button class="layui-btn search_btn" lay-submit="" lay-filter="addBorrow">立即添加</button>
		</div>
	</div>
</form>
<script src="${webroot}/static/js/jquery.min.js" charset="utf-8"></script>
<script type="text/javascript" src="${webroot}/static/layui/layui.js"></script>
<script>
layui.use(['form','layer','laydate'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;
    form.verify({
    	totalMoney:[
            /^[+]{0,1}(\d+)$/
            ,'金额只能为正整数'
           ],
        totalAccrual:[
            /^([0-9][0-9]*)+(.[0-9]{1,2})?$/
            ,'格式输入不正确'
           ],
    });
    laydate.render({
        elem: '#startTime',
        value: new Date(),
        done: function(value, date, endDate){
        	getDate(6,new Date(value));
        }
    });
    laydate.render({
        elem: '#endTime',
        value: getDate(6,new Date())
    });
    
    function getDate(dayNumber,date){
    	date = date ? date : new Date();
	    var ms = dayNumber * (1000 * 60 * 60 * 24)
	    var newDate = new Date(date.getTime() + ms);
	    $("#endTime").val(formatDate(newDate));
    }
    
    function formatDate(date) {
	    var myyear = date.getFullYear();
	    var mymonth = date.getMonth() + 1;
	    var myweekday = date.getDate();
	    if (mymonth < 10) {
	        mymonth = "0" + mymonth;
	    }
	    if (myweekday < 10) {
	        myweekday = "0" + myweekday;
	    }
	    return (myyear + "-" + mymonth + "-" + myweekday);
	}
    
    form.on("submit(addBorrow)",function(data){
        //弹出loading
    	var url =""; var record="";
    	if($("#id").val() == ""){
    		url="/admin/borrow/update";
    		record="编辑借款:";
    	}else{
    		url="/admin/borrow/insert";
    		record="添加借款:";
    	}
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        $.post(url,{
        	 userId  : $("#id").val(),
        	 totalMoney : $(".totalMoney").val(),  
        	 totalAccrual : $(".totalAccrual").val(),
        	 startTime : $("#startTime").val(),
        	 endTime : $("#endTime").val(),
        	 businessMan: $(".businessMan").val(),  
             channelId: data.field.channelId,
             lendingId: data.field.lendingId, 
             record : record
         },function(res){
        	 var data=JSON.parse(res);
        	 if(data.success){
        		top.layer.close(index);
  	            top.layer.msg("操作成功");
        		 setTimeout(function(){
        	            window.location.href="/admin/borrow/detail";
        	        },2000); 
        	 }else{
        		 top.layer.msg(data.message);
        	 }
        })
        return false;
    })
    
      $(".layui-btn-normal").on("click",function(){
    	  var index = layui.layer.open({
              title : "选择借款人",
              type : 2,
	          area : ["700px","450px"],
	          content : "/admin/borrow/selectlist",
          })
      })

    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());
})
function close_self(){
	window.location.href="/admin/userinfo/detail";
}
</script>
</body>
</html>